﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebDomain.Concrete;
using WebDomain.Entities;
using CZTopComputers.Models;

namespace CZTopComputers.Controllers
{
    public class CustomPCController : Controller
    {
        private EFCustomPCRepository pcRepo;
        private EFProductRepository prodRepo;
        private EFTicketRepository ticketRepo;
        private EFTicketContainsRepository ticketContainsRepo;
        private CustomPCViewModel viewModel;

        public CustomPCController()
        {
            pcRepo = new EFCustomPCRepository();
            prodRepo = new EFProductRepository();
            ticketRepo = new EFTicketRepository();
            ticketContainsRepo = new EFTicketContainsRepository();
            viewModel = new CustomPCViewModel();
            viewModel.Products = prodRepo.Products;
        }

        private IEnumerable<Product> ProductsByType(string type)
        {
            return prodRepo.Products.Where(x => x.Type == type);
        }

        public ActionResult BuildIt()
        {
            return View();
        }

        private CustomPC GetPC()
        {
            CustomPC pc = (CustomPC)Session["CustomPC"];
            if (pc == null)
            {
                pc = new CustomPC();
                Session["CustomPC"] = pc;
            }
            return pc;
        }

        public ActionResult Case()
        {
            return View(prodRepo.Products.Where(x=>x.Type=="Case"));
        }

        
        public ActionResult SubmitCase(int id)
        {
            GetPC().CaseID = id;
            return RedirectToAction("Motherboard");
        }

        public ActionResult Motherboard()
        {
            return View(prodRepo.Products.Where(x => x.Type == "Motherboard"));
        }

        public ActionResult SubmitMotherboard(int id)
        {
            GetPC().MotherboardID = id;
            return RedirectToAction("Processor");
        }

        public ActionResult Processor()
        {
            return View(prodRepo.Products.Where(x => x.Type == "Processor"));
        }


        public ActionResult SubmitProcessor(int id)
        {
            GetPC().ProcessorID = id;
            return RedirectToAction("Memory");
        }

        public ActionResult Memory()
        {
            return View(prodRepo.Products.Where(x => x.Type == "Memory"));
        }

        public ActionResult SubmitMemory(int id, int qty)
        {
            GetPC().MemoryID = id;
            GetPC().MemoryQty = qty;
            return RedirectToAction("GraphicsCard");
        }

        public ActionResult GraphicsCard()
        {
            return View(prodRepo.Products.Where(x => x.Type == "Graphics Card"));
        }

        public ActionResult SubmitGraphicsCard(int id, int qty)
        {
            GetPC().GraphicsCardID = id;
            GetPC().GraphicsCardQty = qty;
            return RedirectToAction("HardDrive");
        }

        public ActionResult HardDrive()
        {
            return View(prodRepo.Products.Where(x => x.Type == "Hard Drive"));
        }

        
        public ActionResult SubmitHardDrive(int id, int qty)
        {
            GetPC().HardDriveID = id;
            GetPC().HardDriveQty = qty;
            return RedirectToAction("PowerSupply");
        }

        public ActionResult PowerSupply()
        {
            return View(prodRepo.Products.Where(x => x.Type == "Power Supply"));
        }

        
        public ActionResult SubmitPowerSupply(int id)
        {
            GetPC().PowerSupplyID = id;
            return RedirectToAction("MediaDrive");
        }

        public ActionResult MediaDrive()
        {
            return View(prodRepo.Products.Where(x => x.Type == "Media Drive"));
        }

        
        public ActionResult SubmitMediaDrive(int id)
        {
            GetPC().MediaDriveID = id;
            return RedirectToAction("BuildIt");
        }

        [HttpPost]
        public ActionResult BuildIt(string Name, string Phone)
        {
            if (Name == string.Empty || Name == null || Phone == string.Empty || Phone == null)
                TempData["message"] = "You must have a name and phone number";
            CustomPC pc = GetPC();
            pcRepo.SaveCustomPC(pc);
            int pcID = pcRepo.GetPC();
            Ticket ticket = new Ticket
            {
                CustomerName = Name + " : For CustomPC",
                CustomerPhone = Phone
            };
            ticketRepo.SaveTicket(ticket);
            int tickID = ticketRepo.GetTicket();
            ticketContainsRepo.SaveTicketContain(new TicketContain
            {TicketID=tickID,
                CustomPCID=pcID
            });

            return RedirectToAction("Index", "Home");
        }

    }
}